#ifndef I2CSciptH
#define I2CSciptH
#include <list>
#include <vector>
#include "CmdFactory.h"

class I2CScript
{
public:
	I2CScript(II2CRW *i2crw, const std::string& text) : m_i2crw(i2crw), m_errCode(1)
	{
		stringSplit(m_scripts, text, "\n");

		for (auto& t : m_scripts)
		{
			Command *cmd = CmdFactory::getInstance().createCmd(m_i2crw, &m_vmp, t);
			if (nullptr == cmd) 
			{
				m_errMsg = "[" + t + "] unknow command";
				return;
			}

			m_cmds.push_back(cmd);
		}

		m_errCode = 0;
	}

	~I2CScript()
	{
		for (auto& t : m_cmds)
			delete t;
	}

	int execute() 
	{
		for (auto& t : m_cmds)
		{
			if (t->execute()) 
			{
				return 1;
			}			
		}

		return 0;
	}

	void print()
	{
		for (auto& t : m_scripts)
			std::cout << t << std::endl;
	}

	const std::string& getErrMsg() const { return m_errMsg; }

	const int& getErrCode() const { return m_errCode; }

private:
	std::list<Command*> m_cmds;
	std::vector<std::string> m_scripts;
	II2CRW *m_i2crw;
	VarMap m_vmp;
	std::string m_errMsg;
	int m_errCode;
};


#endif // !I2CSciptH

